package com.lk.api.app;

import android.app.Activity;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.Toast;

import com.lk.api.R;

/**
 * Created by lk on 2014/12/22.
 */
public class MyDBStorage extends Activity {
    SharedPreferences sharedPreferences;
    EditText editText;
    CheckBox checkBox;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.mydbstorage);

        checkBox = (CheckBox) findViewById(R.id.myCheckBox);
        editText = (EditText) findViewById(R.id.myEditText);

        //得到Preferences对象
//        sharedPreferences = getSharedPreferences("myfile", MODE_PRIVATE);

//        还有一种方式可以得到 并以当前的activity名字 做为xml名字并创建
//        sharedPreferences=getPreferences(MODE_PRIVATE);

//        还有一种方式也可以得到
//        整个应用的包名
        sharedPreferences = PreferenceManager.getDefaultSharedPreferences(MyDBStorage.this);

        if (sharedPreferences != null) {
            editText.setText(sharedPreferences.getString("Remark", ""));
            checkBox.setChecked(sharedPreferences.getBoolean("Wifi", false));
        } else {
            Log.d("没有", "xml");
        }


        Button button = (Button) findViewById(R.id.myButton);

        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //  打开编辑
                SharedPreferences.Editor editor = sharedPreferences.edit();
                editor.putBoolean("Wifi", checkBox.isChecked());
                editor.putString("Remark", editText.getText().toString());
                editor.commit();
                finish();
                Toast.makeText(MyDBStorage.this, "保存成功", Toast.LENGTH_LONG).show();
            }
        });
    }
}
